package com.portal.framework.dao;

import java.io.Serializable;

import com.portal.framework.util.ObjectUtil;


/**
 * POJO对象的基类,提供得到这个POJO对象的ID的值,和实现基本的equals(),和 hashCode方法
 * 
 * @author 卢烁波
 * @date 2007-3-10
 */
public abstract class PersistenceObject implements Serializable {

	private transient int hashCode = Integer.MIN_VALUE;

	public PersistenceObject() {
		super();
	}

	/**
	 * 得到这个POJO对象的ID的值
	 * 
	 * @return
	 */
	public abstract Serializable getPrimaryKeyValue();

	public boolean equals(Object obj) {
		if (null == obj)
			return false;
		if (!(obj instanceof PersistenceObject))
			return false;

		PersistenceObject mObj = (PersistenceObject) obj;
		return ObjectUtil.equals(this.getPrimaryKeyValue(), mObj
				.getPrimaryKeyValue());

	}

	public int hashCode() {
		if (Integer.MIN_VALUE == this.hashCode) {
			Serializable pk = this.getPrimaryKeyValue();
			this.hashCode = null == pk ? super.hashCode() : pk.hashCode();
		}
		return this.hashCode;
	}

}
